package com.zhouxujing.game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class MyPlane extends Plane {
	//上下左右
	private boolean up=false;
	private boolean down=false;
	private boolean left=false;
	private boolean right=false;
	private boolean accelerate=false;
	public static int step = MyGameFrame.step;
	public static int bulletX = 19;
	public static int bulletY =20;
	public static int count = 3;//这个count用于判断按2是否改变飞机形态
	public static int count_shield = 3;//用于判断按3是否开启护盾
	public static int count_Tenspeed = 0;//用于判断十倍攻速是否开启
	public static String planepath = "";
	public static String bulletPath = "";
	Evolution evolution = new Evolution();
	public static boolean evolution_flag = false;//判断是否要绘制进化
	public static int how = 1;//表示已变身，2表示未变身；
	int x  = 0;
	public static int attackspeed = Constant.MY_ATTACKSPEED;//我方战机速度
	public static boolean tenSpeed =true;//十倍攻速一局只能开启一次
	
	Blood blood=new Blood();
	
	//myplane类中主要设置的是我方飞机的属性及方法，plane类是飞机类包含敌机与我方飞机共同属性方法；
	public MyPlane(Image img, double x, double y) {
		super(img, x, y);
		setPosition(300, 450);//设置我方战机初始位置
		for(int i=0;i<bullets.length;i++){//
			bullets[i]=new PlaneBullet();
		}
	}
	public MyPlane(Image img, double x, double y,int bulletcount) {
		super(img, x, y,bulletcount);
		setBulletCount(bulletcount);
		setPosition(x, y);//设置我方战机初始位置
		for(int i=0;i<bullets.length;i++){
			bullets[i]=new PlaneBullet();
		}
	}
	public MyPlane(Image img, double x, double y,int bulletcount,int step) {
		super(img, x, y,bulletcount,step);
		this.step=step;
		setBulletCount(bulletcount);
		setPosition(x, y);//设置我方战机初始位置
		for(int i=0;i<bullets.length;i++){
			bullets[i]=new PlaneBullet();
		}
	}
	//绘画我方战机，如果是存活状态就绘画，添加一个变身的gif动图，当我方飞机按2时调用
	public void drawSelf(Graphics g) {
		if(isLive()){
			g.drawImage(getImg(),(int)getX(),(int)getY(), null);
			move();
		}
		if (evolution_flag && MyGameFrame.state == 1) {
			if (Evolution.count<13) {//小于13表示动图未画完
				this.evolution.setLocation(this.getX(), this.getY());//设置位置
				this.evolution.draw(g);
			}
			else {
				Evolution.count=0;
				evolution_flag = false;
			}
			
		}
		//否则调用下面这个方法
		checkLocation();
	}
	/**
	 * 改变方向w , s ,a ,d ,shift
	 * 控制方向和加速
	 * 按下空格键暂停游戏e.getKeyCode()==KeyEvent.VK_SPACE
	 * @param e
	 * 
	 */
	public void stopGame(KeyEvent e) {
		//按下空格暂停游戏
		 if ( e.getKeyCode()==KeyEvent.VK_SPACE){
			 System.out.println("监听空格键");
            switch (MyGameFrame.state){
                //运行改为暂停
                case 1:
               
                    MyGameFrame.state = 2;
               	 System.out.println( MyGameFrame.state);
                    break;
                    //暂停改为运行
                case 2:
                    MyGameFrame.state = 1;
               	 System.out.println( MyGameFrame.state);
                    break;
            }
       }
	}
	public void evolution(KeyEvent e) {
	
			 if ( e.getKeyCode()==KeyEvent.VK_2) {
				 //按下2键变身！
					
				if (MyGameFrame.state == 1 ) {
					evolution_flag = true;
					if (count %2 != 0) {
						planepath = "images/planeex.png";
						bulletPath = "images/bulletx.png";
						x = 10;
						count+=1;
						System.out.println("此时count的值："+count);
					}
					else if(count %2 == 0) {
						planepath = "images/planex.png";
						bulletPath = "images/bullet.png";
						x = 19;
						count+=1;
						System.out.println("此时count的值："+count);
					}
					MyGameFrame.plane = new MyPlane(GameUtil.getImage(planepath), this.getX(), this.getY(), Constant.DEFAULT_PLANE_BC, step);
					MyGameFrame.planefire = new Thread(MyGameFrame.plane);
					PlaneBullet.bullet_img = GameUtil.getImage(bulletPath);
					MyPlane.bulletX=x;//修改子弹发射位置，让它从 飞机尖发射
					MyGameFrame.planefire.start();
					
					
				}
			}
		
		
	}
	
	public void shieldPlane(KeyEvent e) {
		//按下3键开启无敌护盾
		if(e.getKeyCode() == KeyEvent.VK_3) {
			if (MyGameFrame.state == 1 ) {
				if (count_shield %2 != 0) {
					planepath = "images/shieldplane.png";
					//bulletPath = "images/bullet.png";
					x = 20;//改变子弹出现x的坐标
					count_shield+=1;
					System.out.println("此时count的值："+count);
					Blood.buckle=0;
					System.out.println("此时每次受伤扣血："+Blood.buckle);
				}
				else if(count_shield %2 == 0) {
					planepath = "images/planeex.png";
					//bulletPath = "images/bullet.png";
					x = 10;
					count_shield+=1;
					System.out.println("此时count的值："+count);
					Blood.buckle=1;
					System.out.println("此时每次受伤扣血："+Blood.buckle);
				}
				MyGameFrame.plane = new MyPlane(GameUtil.getImage(planepath), this.getX(), this.getY(), Constant.DEFAULT_PLANE_BC, step);
				MyGameFrame.planefire = new Thread(MyGameFrame.plane);
				//PlaneBullet.bullet_img = GameUtil.getImage(bulletPath);
				MyPlane.bulletX=x;//修改子弹发射位置，让它从 飞机尖发射
				MyGameFrame.planefire.start();
			}
		}
	}
public void tenAttackspeed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_4) {
			if (MyGameFrame.state == 1) {
				//count_Tenspeed 自增设置在myplane fire方法中
				if (count_Tenspeed<=2000 && tenSpeed) {
					this.attackspeed=MyPlane.attackspeed/10;
					Constant.DEFAULT_PLANE_BC=Constant.DEFAULT_PLANE_BC;
					System.out.println("开启十倍攻速后我方子弹容量："+Constant.DEFAULT_PLANE_BC);
					System.out.println("我方战机开启十倍攻速！");
					tenSpeed = false;
				}
				
					
				}
			}
		}
	
	public void addDirection(KeyEvent e){
		
		
		switch(e.getKeyCode()){
		case KeyEvent.VK_W:
			up=true;
			break;
		case KeyEvent.VK_S:
			down=true;
			break;
		case KeyEvent.VK_A:
			left=true;
			break;
		case KeyEvent.VK_D:
			right=true;
			break;
		case KeyEvent.VK_1:
			accelerate=true;
			break;
		default:
			System.out.println("没有键");
			break;
		
	}
		}
	/**
	 * 取消某键 停止移动
	 * @param e
	 */
	public void minusDirection(KeyEvent e){
		switch(e.getKeyCode()){
		case KeyEvent.VK_W:
			up=false;
			break;
		case KeyEvent.VK_S:
			down=false;
			break;
		case KeyEvent.VK_A:
			left=false;
			break;
		case KeyEvent.VK_D:
			right=false;
			break;
		case KeyEvent.VK_1:
			accelerate=false;
		
			break;
		}
	}
	//移动我方飞机的位置
	public void move( ) {
		if(accelerate){
			step= step+1;//按下1键移动速度提升
			//System.out.println("MYPlane:step"+step);
		}
		else{
		    step=step;
		}
		if(up){
			moveY(getY()-step);
		}
		if(down){
			moveY(getY()+step);
		}
		if(right){
			moveX(getX()+step);
		}
		if(left){
			moveX(getX()-step);
		}

	}
	/*
	 * 不需要自己回收子弹，因为子弹每次画自己的时�?�都会检查自己的状�??
	 * 如果飞出框外自动回收，设live为false
	 * 检查我方战机的位置，如果超过边界值，就把坐标值改变
	 */
	public void checkLocation(){
		if(getX()<=0) moveX(0);
		if(getX()>=(Constant.GAME_WIDTH-64)) moveX(Constant.GAME_WIDTH-64);
		if(getY()<30) moveY(30);
		if(getY()>=(Constant.GAME_HEIGHT-64)) moveY(Constant.GAME_HEIGHT-64);
	}
//设置子弹的位置
	public void fire() {
		if (count_Tenspeed==800 ) {
			this.attackspeed=attackspeed*10;
			//Constant.DEFAULT_PLANE_BC=Constant.DEFAULT_PLANE_BC;
			System.out.println("已关闭十倍攻速当前攻速和子弹容量"+attackspeed+" "+Constant.DEFAULT_PLANE_BC);
		}
		System.out.println("我方子弹容量 和攻速："+Constant.DEFAULT_PLANE_BC+" "+attackspeed);
		if (MyGameFrame.state ==1) {
			count_Tenspeed++;//开启十倍攻速的计时器
		}
		System.out.println("开启十倍攻速的计时器:"+count_Tenspeed);
		for(int i=0;i<bullets.length;i++){//我方子弹消失，就出现一枚
			if(!bullets[i].isLive()){
				bullets[i].setPosition(getX()+bulletX, getY()-bulletY);
				bullets[i].setLive(true);
				break;
			}
		}
	}
	
	public void run() {
		while(true){
			fire();
			try{//发射子弹间隔
				Thread.sleep(attackspeed);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
}
